from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.

from book.models import Book


def index(request):
    # 添加表记录
    # 方式一
    # book_obj = Book(id=1, title='Python红宝书', price=100, pub_date="2012-12-12", publish='人民出版社')
    # book_obj.save()
    # return HttpResponse('OK')
    # 方式二
    # book_obj = Book.objects.create(title='PHP', price=100, pub_date="2013-12-12", publish='人民出版社')
    # print(book_obj.title)
    # print(book_obj.price)
    # print(book_obj.pub_date)
    # print(book_obj.publish)
    # ========== 查询表记录API ==========
    """
    1 方法的返回值
    2 方法的调用者
    """
    # (1)all方法，返回queryset对象

    # book_list = Book.objects.all()  # <QuerySet [<Book: Python红宝书>, <Book: PHP>]>
    # print(book_list)
    # for obj in book_list:
    #     print(obj.title, obj.price)
    # print(book_list[1].title)
    # (2)first、last：调用者：queryset对象 返回值：model对象
    # book = Book.objects.all().first() # <class 'book.models.Book'>
    # book = Book.objects.all()[0]

    # (3) filter方法，返回值：queryset对象
    # book_list = Book.objects.filter(price=100) # <QuerySet [<Book: Python红宝书>, <Book: PHP>]>
    # print(book_list)
    # book_obj = Book.objects.filter(price=100).first()

    # (4) get方法，有且只有一个查询结果时才有意义，返回值：model对象
    # book_obj = Book.objects.get(title='go')
    # book_obj = Book.objects.get(price=100)  # get() returned more than one Book -- it returned 2!
    # print(book_obj.price)

    # (5) exclude 返回值：queryset对象
    # exclude
    # ret = Book.objects.exclude(title='go') # <QuerySet [<Book: Python红宝书>, <Book: PHP>]>
    # print(ret)

    # (6) order_by 调用者：queryset对象 返回值：queryset对象
    # ret = Book.objects.all().order_by('id')  # <QuerySet [<Book: Python红宝书>, <Book: PHP>, <Book: go>]>
    # ret = Book.objects.all().order_by('-id')  # <QuerySet [<Book: go>, <Book: PHP>, <Book: Python红宝书>]>
    # print(ret)

    # (7) count() 调用者：queryset对象 返回值：int
    # ret = Book.objects.all().count()  # 3
    # print(ret)

    # (8) exist()
    # ret = Book.objects.all().exists() # True
    # print(ret)

    # (9) values方法，调用者：queryset对象 返回值：queryset对象
    # ret = Book.objects.all()
    # for i in ret:
    #     print(i.title, end=', ') # Python红宝书, PHP, go,

    # ret = Book.objects.all().values('price')
    # <QuerySet [{'price': Decimal('100.00')}, {'price': Decimal('100.00')}, {'price': Decimal('200.00')}]>
    # ret = Book.objects.all().values('price', 'title')
    # <QuerySet [{'price': Decimal('100.00'), 'title': 'Python红宝书'}, {'price': Decimal('100.00'), 'title': 'PHP'}, {'price': Decimal('200.00'), 'title': 'go'}]>
    # print(ret)

    # (10) values_list方法 调用者：queryset对象 返回值：queryset对象
    # ret = Book.objects.all().values_list('price', 'title')
    # <QuerySet [(Decimal('100.00'), 'Python红宝书'), (Decimal('100.00'), 'PHP'), (Decimal('200.00'), 'go')]>
    # print(ret)

    # (11) distinct方法
    # ret = Book.objects.all().distinct()
    # <QuerySet [<Book: Python红宝书>, <Book: PHP>, <Book: go>]>
    # print(ret)
    # ret = Book.objects.all().values('price').distinct()
    # <QuerySet [{'price': Decimal('100.00')}, {'price': Decimal('200.00')}]>
    # print(ret)

    # obj = Book.objects.all().filter().order_by().filter().reverse().first() # go
    # print(obj)

    # ========== 查询表记录之模糊查询 ==========
    # ret = Book.objects.filter(price__gt=100) # <QuerySet [<Book: go>]>
    # ret = Book.objects.filter(price__gt=10, price__lt=200) # <QuerySet [<Book: Python红宝书>, <Book: PHP>]>
    # ret = Book.objects.filter(title__startswith='P')  # <QuerySet [<Book: Python红宝书>, <Book: PHP>]>
    # ret = Book.objects.filter(title__contains='o')  # <QuerySet [<Book: Python红宝书>, <Book: go>]>
    # ret = Book.objects.filter(title__icontains='H')  # <QuerySet [<Book: Python红宝书>, <Book: PHP>]>
    # ret = Book.objects.filter(price__in=[200, 300])  # <QuerySet [<Book: go>]>
    # ret = Book.objects.filter(pub_date__year=2018)  # <QuerySet [<Book: go>]>
    # ret = Book.objects.filter(pub_date__year=2018, pub_date__month=5)  # <QuerySet [<Book: go>]>
    # print(ret)

    # ========== 删除记录和修改记录 ==========
    # delete方法 调用者：queryset对象 model对象
    # ret = Book.objects.filter(price=300).delete()  # (1, {'book.Book': 1})
    # ret = Book.objects.filter(price=400).first().delete()  # (1, {'book.Book': 1})
    # print(ret)

    # update方法 调用者：queryset对象
    ret = Book.objects.filter(title='PHP2').update(title='PHP')  # 1
    print(ret)

    return HttpResponse('OK')


def query(request):
    # 查询老男孩出版社出版过的价格大于200的书籍
    ret = Book.objects.filter(publish="老男孩出版社", price__gt=200)
    # 查询2017年8月出版的所有以Py开头的书籍名称
    ret = Book.objects.filter(title__startswith='Py', pub_date__year=2017, pub_date__month=8).values('title')
    # 查询价格为50，100或150的所有书籍名称及其出版社名称
    ret = Book.objects.filter(price__in=[50, 100, 150]).values('title', 'publish')
    # 查询价格在100到200之间的所有书籍名称及其价格
    ret = Book.objects.filter(price__range=[100, 200]).values('title', 'publish')
    # 查询所有人民出版社出版的书籍的价格（从高到低排序，去重）
    ret = Book.objects.filter(publish='人民出版社').values('price').distinct().order_by('-price')
    print(ret)
    return HttpResponse('OK')


def addbook(request):
    return None


def books(request):
    return None


def delbook(request):
    return None


def changebook(request):
    return None